import numpy as np

from core.variable import Variable


# 复杂函数
def sphere(x, y):
    z = x ** 2 + y ** 2
    return z


def matyas(x, y):
    z = 0.26 * (x ** 2 + y ** 2) - 0.48 * x * y
    return z


def goldstein(x, y):
    z = (1 + (x + y + 1) ** 2 * (19 - 14 * x + 3 * x ** 2 - 14 * y + 6 * x * y + 3 * y ** 2)) * \
        (30 + (2 * x - 3 * y) ** 2 * (18 - 32 * x + 12 * x ** 2 + 48 * y - 36 * x * y + 27 * y ** 2))
    return z


def rosenbrock(x0, x1):
    y = 100 * (x1 - x0 ** 2) ** 2 + (x0 - 1) ** 2
    return y


if __name__ == '__main__':
    x = Variable(np.array(1.0))
    y = Variable(np.array(1.0))
    z = sphere(x, y)  # sphere(x, y) / matyas(x, y)
    z.backward()
    print(x.grad.data, y.grad.data)

    # x.name = 'x'
    # y.name = 'y'
    # z.name = 'z'
    # plot_dot_graph(z, verbose=False, to_file='goldstein.png')
